﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace 物流管理系统
{
    public partial class Infoqueryform : Form
    {
        DBOperation dboperation = new DBOperation();
        public int tableselect;
        string tablename;
        string columnname;
        string sqlstr;
        ReMatch re = new ReMatch();
        public Infoqueryform()
        {
            InitializeComponent();
        }
        private void baseinfoqueryform_Load(object sender, EventArgs e)
        {
            switch (tableselect)
            {
                case 0:
                    {
                        tablename="货物信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("货物条形码");
                        querycolumnselect.Items.Add("货物名称");
                        querycolumnselect.Items.Add("产地");
                        querycolumnselect.Items.Add("单位");
                        querycolumnselect.Items.Add("进价");
                        querycolumnselect.Items.Add("售价");
                        querycolumnselect.Items.Add("备注");
                        break;
                    }
                case 1:
                    {
                        tablename="车辆信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("车牌号");
                        querycolumnselect.Items.Add("型号");
                        querycolumnselect.Items.Add("驾驶员编号");
                        querycolumnselect.Items.Add("载重量");
                        querycolumnselect.Items.Add("是否可用");
                        querycolumnselect.Items.Add("发车站");
                        querycolumnselect.Items.Add("是否装货");
                        break;
                    }
                case 2:
                    {
                        tablename="客户信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("客户编号");
                        querycolumnselect.Items.Add("客户姓名");
                        querycolumnselect.Items.Add("开户银行");
                        querycolumnselect.Items.Add("联系地址");
                        querycolumnselect.Items.Add("联系电话");
                        querycolumnselect.Items.Add("所属会员类型");
                        querycolumnselect.Items.Add("银行账户");
                        break;
                    }
                case 3:
                    {
                        tablename="驾驶员信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("驾驶员编号");
                        querycolumnselect.Items.Add("驾驶员姓名");
                        querycolumnselect.Items.Add("出生年月");
                        querycolumnselect.Items.Add("性别");
                        querycolumnselect.Items.Add("驾龄");
                        querycolumnselect.Items.Add("籍贯");
                        querycolumnselect.Items.Add("备注");
                        querycolumnselect.Items.Add("驾车类型");
                        querycolumnselect.Items.Add("是否空闲");
                        break;
                    }
                case 4:
                    {
                        tablename="会员类型信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("会员类型名");
                        querycolumnselect.Items.Add("折扣");
                        break;
                    }
                case 5:
                    {
                        tablename="用户信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("用户编号");
                        querycolumnselect.Items.Add("用户密码");
                        querycolumnselect.Items.Add("车辆信息管理权限");
                        querycolumnselect.Items.Add("货物信息管理权限");
                        querycolumnselect.Items.Add("客户信息管理权限");
                        querycolumnselect.Items.Add("驾驶员信息管理权限");
                        querycolumnselect.Items.Add("会员类型信息管理权限");
                        querycolumnselect.Items.Add("用户信息管理权限");
                        querycolumnselect.Items.Add("托运信息登记权限");
                        querycolumnselect.Items.Add("查看托运信息权限");
                        querycolumnselect.Items.Add("装车登记权限");
                        querycolumnselect.Items.Add("查看装车信息权限");
                        querycolumnselect.Items.Add("修改装车信息权限");
                        querycolumnselect.Items.Add("发车登记权限");
                        querycolumnselect.Items.Add("车辆到达登记权限");
                        querycolumnselect.Items.Add("车辆返回登记权限");
                        querycolumnselect.Items.Add("查看发车信息权限");
                        querycolumnselect.Items.Add("修改发车信息权限");
                        querycolumnselect.Items.Add("到货登记权限");
                        querycolumnselect.Items.Add("货到确认权限");
                        querycolumnselect.Items.Add("修改到货信息权限");
                        break;
                    }
                case 6:
                    {
                        tablename="托运信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("托运编号");
                        querycolumnselect.Items.Add("托运日期");
                        querycolumnselect.Items.Add("货物编号");
                        querycolumnselect.Items.Add("托运件数");
                        querycolumnselect.Items.Add("重量（千克）");
                        querycolumnselect.Items.Add("货物运价");
                        querycolumnselect.Items.Add("费用");
                        querycolumnselect.Items.Add("客户编号");
                        querycolumnselect.Items.Add("发货地");
                        querycolumnselect.Items.Add("收货地");
                        querycolumnselect.Items.Add("收货人姓名");
                        querycolumnselect.Items.Add("收货人电话");
                        querycolumnselect.Items.Add("托运状态");
                        querycolumnselect.Items.Add("备注");
                        break;
                    }
                case 7:
                    {
                        tablename="装车信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("装车编号");
                        querycolumnselect.Items.Add("托运编号");
                        querycolumnselect.Items.Add("车牌号");
                        querycolumnselect.Items.Add("装车日期");
                        querycolumnselect.Items.Add("备注");
                        break;
                    }
                case 8:
                    {
                        tablename="发车信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("发车编号");
                        querycolumnselect.Items.Add("车牌号");
                        querycolumnselect.Items.Add("发车日期");
                        querycolumnselect.Items.Add("是否到达");
                        querycolumnselect.Items.Add("到达日期");
                        querycolumnselect.Items.Add("是否返回");
                        querycolumnselect.Items.Add("返回日期");
                        break;
                    }
                case 9:
                    {
                        tablename="收货信息表";
                        querycolumnselect.SelectedIndex = querycolumnselect.Items.IndexOf("ALL");
                        querycolumnselect.Items.Add("收货编号");
                        querycolumnselect.Items.Add("托运编号");
                        querycolumnselect.Items.Add("收货日期");
                        querycolumnselect.Items.Add("备注");
                        querycolumnselect.Items.Add("付款方式");
                        querycolumnselect.Items.Add("付款日期");
                        break;
                    }
            }
            selectedtablelabel.Text =tablename;
        }
        private void query_Click(object sender, EventArgs e) => sqlquery();
        private void add_Click(object sender, EventArgs e) => sqladd();
        private void change_Click(object sender, EventArgs e) => listdetail(1);
        private void delete_Click(object sender, EventArgs e) => sqldelete();
        private void quit_Click(object sender, EventArgs e) => Close();
        private void sqlquery()
        {
            if (querycolumnselect.SelectedIndex == 0)
            {
                sqlstr= "select * from "+tablename;
            }
            else
            {
                columnname=querycolumnselect.Text;
                if (keywordtext.Text.Equals("")|| keywordtext.Text.Equals("请输入列关键字"))
                {
                    MessageBox.Show("请输入列关键字！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (re.specialmatch(keywordtext.Text))
                {
                    MessageBox.Show("非法的关键字！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                sqlstr= "select * from "+tablename+" where "+columnname+" like "+"'%"+keywordtext.Text+"%'";
            }
            datatable.DataSource = dboperation.querysql(sqlstr, tablename);
            datatable.DataMember =tablename;
            alldatalabel.Text= datatable.RowCount.ToString();
        }
        private void sqladd() => listdetail(0);
        private void listdetail(int formtypeselect)
        {
            if (datatable.SelectedRows.Count!=1&&formtypeselect!=0)
            {
                MessageBox.Show("请选中一行！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                switch (tableselect)
                {
                    case 0:
                        {
                            Goodsinfo goodsinfo = new Goodsinfo();
                            goodsinfo.formtype=formtypeselect;
                            if (formtypeselect!=0)
                            {
                                goodsinfo.idtext.Text=datatable.SelectedRows[0].Cells[0].Value.ToString();
                                goodsinfo.nametext.Text=datatable.SelectedRows[0].Cells[1].Value.ToString();
                                goodsinfo.origintext.Text=datatable.SelectedRows[0].Cells[2].Value.ToString();
                                goodsinfo.companytext.Text=datatable.SelectedRows[0].Cells[3].Value.ToString();
                                goodsinfo.costtext.Text=datatable.SelectedRows[0].Cells[4].Value.ToString();
                                goodsinfo.pricetext.Text=datatable.SelectedRows[0].Cells[5].Value.ToString();
                                goodsinfo.remarkstext.Text=datatable.SelectedRows[0].Cells[6].Value.ToString();
                            }
                            goodsinfo.ShowDialog();
                            break;
                        }
                    case 1:
                        {
                            Carsinfo carsinfo = new Carsinfo();
                            carsinfo.formtype=formtypeselect;
                            List<string> driveridlist = dboperation.querylist("select 驾驶员编号 from 驾驶员信息表");
                            setcombobox(carsinfo.driveridcombobox, driveridlist);
                            if (formtypeselect!=0)
                            {
                                carsinfo.idtext.Text=datatable.SelectedRows[0].Cells[0].Value.ToString();
                                carsinfo.modeltext.Text=datatable.SelectedRows[0].Cells[1].Value.ToString();
                                carsinfo.driveridcombobox.Text=datatable.SelectedRows[0].Cells[2].Value.ToString();
                                carsinfo.loadcapacitytext.Text=datatable.SelectedRows[0].Cells[3].Value.ToString();
                                carsinfo.startpointtext.Text=datatable.SelectedRows[0].Cells[4].Value.ToString();
                                setcheckbox(carsinfo.loadcheckbox, datatable.SelectedRows[0].Cells[5].Value.ToString());
                                setcheckbox(carsinfo.availablecheckbox, datatable.SelectedRows[0].Cells[6].Value.ToString());
                            }
                            carsinfo.ShowDialog();
                            break;
                        }
                    case 2:
                        {
                            Customersinfo customersinfo = new Customersinfo();
                            customersinfo.formtype=formtypeselect;
                            List<string> membertypeidlist = dboperation.querylist("select 会员类型名 from 会员类型信息表");
                            setcombobox(customersinfo.membertypecombobox, membertypeidlist);
                            if (formtypeselect!=0)
                            {
                                customersinfo.idtext.Text=datatable.SelectedRows[0].Cells[0].Value.ToString();
                                customersinfo.nametext.Text=datatable.SelectedRows[0].Cells[1].Value.ToString();
                                customersinfo.banktext.Text=datatable.SelectedRows[0].Cells[2].Value.ToString();
                                customersinfo.addresstext.Text=datatable.SelectedRows[0].Cells[3].Value.ToString();
                                customersinfo.telephonetext.Text=datatable.SelectedRows[0].Cells[4].Value.ToString();
                                customersinfo.membertypecombobox.Text=datatable.SelectedRows[0].Cells[5].Value.ToString();
                                customersinfo.bankaccounttext.Text=datatable.SelectedRows[0].Cells[6].Value.ToString();
                            }
                            customersinfo.ShowDialog();
                            break;
                        }
                    case 3:
                        {
                            Driversinfo driversinfo = new Driversinfo();
                            driversinfo.formtype=formtypeselect;
                            if (formtypeselect!=0)
                            {
                                driversinfo.idtext.Text=datatable.SelectedRows[0].Cells[0].Value.ToString();
                                driversinfo.nametext.Text=datatable.SelectedRows[0].Cells[1].Value.ToString();
                                driversinfo.birthdatepicker.Text=datatable.SelectedRows[0].Cells[2].Value.ToString();
                                driversinfo.sexcombobox.Text=datatable.SelectedRows[0].Cells[3].Value.ToString();
                                driversinfo.drivingagecombobox.Text=datatable.SelectedRows[0].Cells[4].Value.ToString();
                                driversinfo.nativeplace.Text=datatable.SelectedRows[0].Cells[5].Value.ToString();
                                driversinfo.remarkstext.Text=datatable.SelectedRows[0].Cells[6].Value.ToString();
                                driversinfo.cartypetext.Text=datatable.SelectedRows[0].Cells[7].Value.ToString();
                                setcheckbox(driversinfo.availablecheckbox, datatable.SelectedRows[0].Cells[8].Value.ToString());
                            }
                            driversinfo.ShowDialog();
                            break;
                        }
                    case 4:
                        {
                            Membertypeinfo membertypeinfo = new Membertypeinfo();
                            membertypeinfo.formtype = formtypeselect;
                            if (formtypeselect!=0)
                            {
                                membertypeinfo.idtext.Text=datatable.SelectedRows[0].Cells[0].Value.ToString();
                                membertypeinfo.discounttext.Text=datatable.SelectedRows[0].Cells[1].Value.ToString();
                            }
                            membertypeinfo.ShowDialog();
                            break;
                        }
                    case 5:
                        {
                            Usersinfo userinfo = new Usersinfo();
                            userinfo.formtype = formtypeselect;
                            if (formtypeselect!=0)
                            {
                                userinfo.idtext.Text = datatable.SelectedRows[0].Cells[0].Value.ToString();
                                userinfo.passwordtext.Text = datatable.SelectedRows[0].Cells[1].Value.ToString();
                                setcheckbox(userinfo.goodsinfoj, datatable.SelectedRows[0].Cells[2].Value.ToString());
                                setcheckbox(userinfo.carsinfoj, datatable.SelectedRows[0].Cells[3].Value.ToString());
                                setcheckbox(userinfo.customersinfoj, datatable.SelectedRows[0].Cells[4].Value.ToString());
                                setcheckbox(userinfo.driversinfoj, datatable.SelectedRows[0].Cells[5].Value.ToString());
                                setcheckbox(userinfo.membertypeinfoj, datatable.SelectedRows[0].Cells[6].Value.ToString());
                                setcheckbox(userinfo.usersinfoj, datatable.SelectedRows[0].Cells[7].Value.ToString());
                                setcheckbox(userinfo.consignmentinfoj1, datatable.SelectedRows[0].Cells[8].Value.ToString());
                                setcheckbox(userinfo.consignmentinfoj2, datatable.SelectedRows[0].Cells[9].Value.ToString());
                                setcheckbox(userinfo.loadinginfoj1, datatable.SelectedRows[0].Cells[10].Value.ToString());
                                setcheckbox(userinfo.loadinginfoj2, datatable.SelectedRows[0].Cells[11].Value.ToString());
                                setcheckbox(userinfo.loadinginfoj3, datatable.SelectedRows[0].Cells[12].Value.ToString());
                                setcheckbox(userinfo.startinfoj1, datatable.SelectedRows[0].Cells[13].Value.ToString());
                                setcheckbox(userinfo.startinfoj2, datatable.SelectedRows[0].Cells[14].Value.ToString());
                                setcheckbox(userinfo.startinfoj3, datatable.SelectedRows[0].Cells[15].Value.ToString());
                                setcheckbox(userinfo.startinfoj4, datatable.SelectedRows[0].Cells[16].Value.ToString());
                                setcheckbox(userinfo.startinfoj5, datatable.SelectedRows[0].Cells[17].Value.ToString());
                                setcheckbox(userinfo.receivinginfoj1, datatable.SelectedRows[0].Cells[18].Value.ToString());
                                setcheckbox(userinfo.receivinginfoj2, datatable.SelectedRows[0].Cells[19].Value.ToString());
                                setcheckbox(userinfo.receivinginfoj3, datatable.SelectedRows[0].Cells[20].Value.ToString());
                            }
                            userinfo.ShowDialog();
                            break;
                        }
                    case 6:
                        {
                            Consignmentinfo consignmentinfo = new Consignmentinfo();
                            consignmentinfo.formtype=formtypeselect;
                            List<string> goodsidlist = dboperation.querylist("select 货物条形码 from 货物信息表");
                            setcombobox(consignmentinfo.goodsidcombobox, goodsidlist);
                            List<string> customeridlist = dboperation.querylist("select 客户编号 from 客户信息表");
                            setcombobox(consignmentinfo.customeridtext, customeridlist);
                            if (formtypeselect!=0)
                            {
                                consignmentinfo.idtext.Text=datatable.SelectedRows[0].Cells[0].Value.ToString();
                                consignmentinfo.consignmentdatepicker.Text=datatable.SelectedRows[0].Cells[1].Value.ToString();
                                consignmentinfo.quantitytext.Text=datatable.SelectedRows[0].Cells[2].Value.ToString();
                                consignmentinfo.weighttext.Text=datatable.SelectedRows[0].Cells[3].Value.ToString();
                                consignmentinfo.goodsidcombobox.Text=datatable.SelectedRows[0].Cells[4].Value.ToString();
                                consignmentinfo.transpricetext.Text=datatable.SelectedRows[0].Cells[5].Value.ToString();
                                consignmentinfo.costtext.Text=datatable.SelectedRows[0].Cells[6].Value.ToString();
                                consignmentinfo.customeridtext.Text=datatable.SelectedRows[0].Cells[7].Value.ToString();
                                consignmentinfo.startpointtext.Text=datatable.SelectedRows[0].Cells[8].Value.ToString();
                                consignmentinfo.destinationtext.Text=datatable.SelectedRows[0].Cells[9].Value.ToString();
                                consignmentinfo.receivertext.Text=datatable.SelectedRows[0].Cells[10].Value.ToString();
                                consignmentinfo.receiverphonetext.Text=datatable.SelectedRows[0].Cells[11].Value.ToString();
                                consignmentinfo.statecombobox.Text=datatable.SelectedRows[0].Cells[12].Value.ToString();
                                consignmentinfo.remarkstext.Text=datatable.SelectedRows[0].Cells[13].Value.ToString();
                            }
                            consignmentinfo.ShowDialog();
                            break;
                        }
                    case 7:
                        {
                            Loadinginfo loadinginfo = new Loadinginfo();
                            loadinginfo.formtype=formtypeselect;
                            List<string> consignmentidlist = dboperation.querylist("select 托运编号 from 托运信息表 where 托运状态='未装车'");
                            setcombobox(loadinginfo.consignmentidcombobox, consignmentidlist);
                            List<string> caridlist = dboperation.querylist("select 车牌号 from 车辆信息表 where 可用='1'");
                            setcombobox(loadinginfo.caridcombobox, caridlist);
                            if (formtypeselect!=0)
                            {
                                loadinginfo.idtext.Text=datatable.SelectedRows[0].Cells[0].Value.ToString();
                                loadinginfo.consignmentidcombobox.Text=datatable.SelectedRows[0].Cells[1].Value.ToString();
                                loadinginfo.caridcombobox.Text=datatable.SelectedRows[0].Cells[2].Value.ToString();
                                loadinginfo.loadingdatepicker.Text=datatable.SelectedRows[0].Cells[3].Value.ToString();
                                loadinginfo.remarkstext.Text =datatable.SelectedRows[0].Cells[4].Value.ToString();
                            }
                            loadinginfo.ShowDialog();
                            break;
                        }
                    case 8:
                        {
                            Startinfo startinfo = new Startinfo();
                            startinfo.formtype=formtypeselect;
                            List<string> caridlist = dboperation.querylist("select 车牌号 from 车辆信息表");
                            setcombobox(startinfo.caridcombobox, caridlist);
                            if (formtypeselect!=0)
                            {
                                startinfo.idtext.Text = datatable.SelectedRows[0].Cells[0].Value.ToString();
                                startinfo.caridcombobox.Text = datatable.SelectedRows[0].Cells[1].Value.ToString();
                                startinfo.startdatepicker.Text = datatable.SelectedRows[0].Cells[2].Value.ToString();
                                setcheckbox(startinfo.arrivecheckbox, datatable.SelectedRows[0].Cells[3].Value.ToString());
                                startinfo.arrivedatepicker.Text = datatable.SelectedRows[0].Cells[4].Value.ToString();
                                setcheckbox(startinfo.returncheckbox, datatable.SelectedRows[0].Cells[5].Value.ToString());
                                startinfo.returndatepicker.Text = datatable.SelectedRows[0].Cells[6].Value.ToString();

                            }
                            startinfo.ShowDialog();
                            break;
                        }
                    case 9:
                        {
                            Receivinginfo receivinginfo = new Receivinginfo();
                            receivinginfo.formtype=formtypeselect;
                            List<string> consignmentidlist = dboperation.querylist("select 托运编号 from 托运信息表 where 托运状态='已到达'");
                            setcombobox(receivinginfo.consignmentidcombobox, consignmentidlist);
                            if (formtypeselect!=0)
                            {
                                receivinginfo.idtext.Text = datatable.SelectedRows[0].Cells[0].Value.ToString();
                                receivinginfo.consignmentidcombobox.Text = datatable.SelectedRows[0].Cells[1].Value.ToString();
                                receivinginfo.receviedatepicker.Text = datatable.SelectedRows[0].Cells[2].Value.ToString();
                                receivinginfo.paydatepicker.Text = datatable.SelectedRows[0].Cells[3].Value.ToString();
                                receivinginfo.paywaycombobox.Text = datatable.SelectedRows[0].Cells[4].Value.ToString();
                                receivinginfo.remarkstext.Text = datatable.SelectedRows[0].Cells[5].Value.ToString();
                            }
                            receivinginfo.ShowDialog();
                            break;
                        }
                }

            }
        }
        private void sqldelete()
        {
            if (datatable.SelectedRows.Count==0)
            {
                MessageBox.Show("未选中信息！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (MessageBox.Show("是否删除选中的"+datatable.SelectedRows.Count+"条信息？", "确认删除", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    foreach (DataGridViewRow row in datatable.SelectedRows)
                    {
                        sqlstr="delete from "+tablename+" where "+querycolumnselect.Items[1]+" = '"+row.Cells[0].Value+"'";
                        if (row.Selected == true)
                        {
                            dboperation.nonquerysql(sqlstr);
                        }
                    }
                    MessageBox.Show("删除成功！", "删除结果");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除失败！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Console.WriteLine(ex.Message);
                }
            }

        }
        private void setcheckbox(CheckBox box, string textstr)
        {
            if (textstr.Equals("1"))
            {
                box.Checked = true;
            }
            else
            {
                box.Checked = false;
            }
        }
        private void setcombobox(ComboBox box, List<string> list) => box.DataSource = list;
        private void datatable_SelectionChanged(object sender, EventArgs e) => selecteddatalabel.Text=datatable.SelectedRows.Count.ToString();
        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)=> listdetail(1);
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) => sqldelete();
        private void 查看ToolStripMenuItem_Click(object sender, EventArgs e) => listdetail(2);
    }
}
